package com.example.data_save;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import com.example.data_save.util.PermissionUtil;
import com.example.data_save.util.ToastUtil;

/**
  *
  * @description 懒汉式动态申请权限
  * @param 
  * @return 
  * author snjgm
  * time 2022/7/26 上午9:36
  */
public class PermissionLazyActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String[] PERMISSIONS_CONTACTS = new String[]{
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS
    };

    private static final String[] PERMISSIONS_SMS = new String[]{
            Manifest.permission.SEND_SMS,
            Manifest.permission.READ_SMS
    };

    private static final int REQUEST_CODE_CONTACTS = 1;
    private static final int REQUEST_CODE_SMS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_lazy);
        findViewById(R.id.btn_connectInfromation).setOnClickListener(this);
        findViewById(R.id.btn_sendOrReceiverInformation).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_connectInfromation:
                PermissionUtil.checkPermission(this,PERMISSIONS_CONTACTS,REQUEST_CODE_CONTACTS);
                break;
            case R.id.btn_sendOrReceiverInformation:
                PermissionUtil.checkPermission(this,PERMISSIONS_SMS,REQUEST_CODE_SMS);
                break;
        }
    }

    /**
      *
      * @description 判断用户是否打开权限的返回方法
      * @param 
      * @return 
      * author snjgm
      * time 2022/7/25 下午4:41
      */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_CODE_CONTACTS:
                if(PermissionUtil.checkGrant(grantResults)){
                    Log.d("TAG", "通讯录权限获取成功");
                }else{
                    ToastUtil.successShow(this,"通讯录权限获取失败");
                    jumpTosettins();
                }
                break;
            case REQUEST_CODE_SMS:
                if(PermissionUtil.checkGrant(grantResults)){
                    Log.d("TAG", "收发权限成功");
                }else{
                    ToastUtil.successShow(this,"收发权限获取失败");
                    jumpTosettins();
                }
                break;
        }
    }

    /**
      *
      * @description 跳转到应用设置界面
      * @param 
      * @return 
      * author snjgm
      * time 2022/7/25 下午5:25
      */
    private void jumpTosettins(){
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package",getPackageName(),null));

        startActivity(intent);
    }
}